package com.plum.admin.config.swagger;

import com.google.common.collect.Sets;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * TODO
 *
 * @author 子丶沫
 * @version 1.0
 * @date 2021/8/13 10:45
 */
@Configuration
@Slf4j
public class SwaggerConfig {
    @Bean
    public Docket docketApi(){
        log.info("加载Swagger3.........");
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any()).build()
                .protocols(Sets.newHashSet("https","http"))
                .securitySchemes(securityScheme())
                .securityContexts(securityContexts());

    }
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("plum后台管理接口文档")
                .description("plum后台管理接口文档")
                .contact(new Contact("子丶沫","https://www.baidu.com","775771672@163.com"))
                .version("1.0")
                .build();
    }
    private List<SecurityScheme> securityScheme(){
        List<SecurityScheme> securitySchemeList=new ArrayList<>();
        securitySchemeList.add(new ApiKey("Authorization","Authorization","token"));
        return securitySchemeList;
    }
    private List<SecurityContext> securityContexts(){
        List<SecurityContext> securityContexts=new ArrayList<>();
        securityContexts.add(SecurityContext
                .builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.any()).build()
        );
        return securityContexts;
    }
    private List<SecurityReference> defaultAuth(){
        AuthorizationScope authorizationScope=new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes=new AuthorizationScope[1];
        authorizationScopes[0]=authorizationScope;
        List<SecurityReference> securityReferences=new ArrayList<>();
        securityReferences.add(new SecurityReference("Authorization",authorizationScopes));
        return securityReferences;
    }
}
